package jpavlich.cardsorter.action;

import fr.inria.zvtm.animation.Animation;
import fr.inria.zvtm.animation.Animation.Dimension;
import fr.inria.zvtm.animation.EndAction;
import fr.inria.zvtm.engine.VirtualSpaceManager;

public class AnimationEndAction implements EndAction {

	private ActionTimeManager timeMgr;
	private Animation anim;
	private long startTime;

	public AnimationEndAction(Animation next, long startTime, ActionTimeManager timeMgr) {
		super();
		this.anim = next;
		this.startTime = startTime;
		this.timeMgr = timeMgr;
	}

	@Override
	public void execute(Object subject, Dimension dimension) {
		if (anim != null) {
			long timeToStart = timeMgr.getAbsoluteTime(startTime) - System.currentTimeMillis();
			if (timeToStart > 0) {
				try {
					Thread.sleep(timeToStart);
				} catch (InterruptedException e) {
				}
			}
			VirtualSpaceManager.INSTANCE.getAnimationManager().startAnimation(anim, false);
		}
	}

}
